package com.changgou.service.goods.controller;

import com.changgou.common.pojo.PageResult;
import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.pojo.Spec;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/brand")
@RestController //包含2个注解   1.@ResponseBody  2.@Controller
@CrossOrigin //开启跨域请求操作
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping
    public Result<List<Brand>> findBrandList() {
        List<Brand> brandList = brandService.findBrandList();

        return new Result<>(true, StatusCode.OK, "查询成功", brandList);
    }

    /**
     * 根据id查询品牌信息
     */
    @GetMapping("/{id}")
    public Result<Brand> findBrandById(@PathVariable("id") Integer id) {
        Brand brand = brandService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", brand);
    }

    /**
     * 添加品牌
     */

    @PostMapping
    public Result add(@RequestBody Brand brand) {
        brandService.addBrand(brand);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * 修改品牌信息
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Integer id, @RequestBody Brand brand) {
        brand.setId(id);
        brandService.updateBrand(brand);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delById(@PathVariable("id") Integer id) {
        brandService.deleteById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 根据条件查询
     * @param brand
     * @return
     */
    @GetMapping("/search")
    public Result<List<Brand>> search(@RequestBody Brand brand){
        List<Brand> searchBrandList = brandService.search(brand);
        return new Result<>(true,StatusCode.OK,"查询成功",searchBrandList);
    }


    /**
     * 根据条件分页查询
     */
    @GetMapping("/searchPage/{page}/{size}")
    public Result findPage(@PathVariable("page")int page,@PathVariable("size") int size,@RequestBody Brand brand ){
        Page<Brand> brandPage = brandService.findPage(brand, page, size);

        PageResult<Brand> pageResult = new PageResult<>(brandPage.getTotal(),brandPage.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }



    @GetMapping("/searchBrand/{categoryName}")
    public List<Brand> findBrandByCategoryName(@PathVariable("categoryName") String categoryName) {
        return brandService.findBrandByCategoryName(categoryName);
    }





}

